package jmine.tec.scheduling.web.pages.task;

import jmine.tec.scheduling.domain.ScheduledTask;
import jmine.tec.web.wicket.component.MapsWicketDateTextField;
import jmine.tec.web.wicket.component.button.ActionAjaxButton;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

import bancosys.tec.utils.date.Date;

/**
 * @author Willian Gigliotti
 */
public class ModalPanelExecutarTask extends Panel {

    private final ModalWindow modal;

    private Date dataExecucao;

    private final ScheduledTask task;

    private final ListScheduledTaskPage page;

    private Panel feedback;

    /**
     * Construtor
     * 
     * @param window modal
     * @param task task
     * @param page executor
     */
    public ModalPanelExecutarTask(ModalWindow window, ScheduledTask task, ListScheduledTaskPage page) {
        super(window.getContentId());
        this.modal = window;
        this.task = task;
        this.page = page;
        this.createComponentes();

    }

    /**
     * Componentes
     */
    private void createComponentes() {
        Form<ScheduledTask> form = new Form<ScheduledTask>("runForm");
        this.add(form);

        this.feedback = new FeedbackPanel("feedback");
        this.feedback.setOutputMarkupId(true);

        form.add(this.feedback);

        ActionAjaxButton executar = new ActionAjaxButton("executarButton", new Model<String>("Executar")) {

            @Override
            protected void onAction(AjaxRequestTarget target, Form<?> form) throws Exception {
                ModalPanelExecutarTask.this.page
                        .runTask(ModalPanelExecutarTask.this.task, ModalPanelExecutarTask.this.dataExecucao, target);
                ModalPanelExecutarTask.this.modal.close(target);
            }
        };
        form.add(executar);

        AjaxLink<String> cancel = new AjaxLink<String>("cancelButton") {

            @Override
            public void onClick(final AjaxRequestTarget target) {
                ModalPanelExecutarTask.this.modal.close(target);
            }

        };
        form.add(cancel);

        Label label = new Label("taskDesc", this.task.getDescription());
        this.add(label);

        MapsWicketDateTextField txtDataCaptacao =
                MapsWicketDateTextField.forDatePattern("dataExecucao", new PropertyModel<Date>(this, "dataExecucao"), true, true);
        txtDataCaptacao.setRequired(true);
        form.add(txtDataCaptacao);

    }

    /**
     * @param dataExecucao the dataExec to set
     */
    public void setDataExecucao(Date dataExecucao) {
        this.dataExecucao = dataExecucao;
    }

    /**
     * @return the dataExec
     */
    public Date getDataExecucao() {
        return this.dataExecucao;
    }

}
